package com.cyl.wms.controller;

import com.cyl.wms.domain.Loader;
import com.cyl.wms.pojo.query.AddrQuery;
import com.cyl.wms.service.AddrService;
import com.cyl.wms.service.LoaderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 载具Controller
 *
 * @author zcc
 * @date 2022-08-05
 */
@Api("载具接口列表")
@RestController
@RequestMapping("/wms/loader")
public class LoaderController extends BaseController {
    @Autowired
    private LoaderService service;

    @ApiOperation("查询载具列表")
    @PreAuthorize("@ss.hasPermi('wms:loader:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<Loader>> list(@RequestBody AddrQuery query, Pageable page) {
        List<Loader> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出载具列表")
    @PreAuthorize("@ss.hasPermi('wms:loader:export')")
    @Log(title = "载具", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(AddrQuery query) {
        List<Loader> list = service.selectList(query, null);
        ExcelUtil<Loader> util = new ExcelUtil<>(Loader.class);
        return ResponseEntity.ok(util.writeExcel(list, "载具数据"));
    }

    @ApiOperation("获取载具详细信息")
    @PreAuthorize("@ss.hasPermi('wms:loader:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<Loader> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增载具")
    @PreAuthorize("@ss.hasPermi('wms:loader:add')")
    @Log(title = "载具", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody Loader Loader) {
        return ResponseEntity.ok(service.insert(Loader));
    }

    @ApiOperation("修改载具")
    @PreAuthorize("@ss.hasPermi('wms:loader:edit')")
    @Log(title = "载具", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody Loader Loader) {
        return ResponseEntity.ok(service.update(Loader));
    }

    @ApiOperation("删除载具")
    @PreAuthorize("@ss.hasPermi('wms:loader:remove')")
    @Log(title = "载具", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
}
